<?php

function validate_blog_post($blogs_arr) {
	
	$errors = 'Errors in: ';
	$errnum = 0;
	
	if (!$blogs_arr['blog_title']){
		$errors .= 'blog title (empty value), ';
		$errnum++;
	}
	if (!$blogs_arr['blog_text']){
		$errors .= 'blog text (empty value), ';
		$errnum++;
	}
	if ($errnum > 0){
		$result = $errors . ' total ' . $errnum . ' errors <br/> <a href="javascript:history.back(-1)">Back</a>';
	}
	else {
		$result = 1;
	}
	return $result;
}

function delete_blogs($blogs)
{
	DB()->query("DELETE FROM " . TS_BLOGS . " WHERE blog_id IN ($blogs)");
	return;
}

function insert_blogs($blogs_arr)
{
	global $userdata;
	
	$current_time = TIMENOW;
	
	$show_blog_index = $blogs_arr['show_index_blog'];
	$enabled_comments = $blogs_arr['enabled_comment'];
	$blog_title  = DB()->escape($blogs_arr['blog_title']);
	$blog_poster = DB()->escape($blogs_arr['blog_poster']);
	$blog_text  = DB()->escape($blogs_arr['blog_text']);
	
	$column = "show_blog_of_index, enabled_comment, blog_title, blog_text, blog_poster, blog_poster_id, blog_time";
	$value = "$show_blog_index, $enabled_comments, '$blog_title', '$blog_text', '$blog_poster', {$userdata['user_id']}, $current_time";

	DB()->query("INSERT INTO ". TS_BLOGS ." ($column) VALUES ($value)");
}

function update_blogs($blogs_arr)
{
	global $userdata;
	
	$current_time = TIMENOW;
		
	$blog_id = $blogs_arr['blog_id'];
	$show_blog_index = $blogs_arr['show_index_blog'];
	$enabled_comments = $blogs_arr['enabled_comment'];
	$blog_title = DB()->escape($blogs_arr['blog_title']);
	$blog_text = DB()->escape($blogs_arr['blog_text']);
	$blog_poster = DB()->escape($blogs_arr['blog_poster']);
	
	DB()->query("UPDATE " . TS_BLOGS . " SET
		show_blog_of_index 	= '$show_blog_index',
		enabled_comment		= '$enabled_comments',
		blog_title   		= '$blog_title',
		blog_text    		= '$blog_text',
		blog_poster  		= '$blog_poster',
		blog_poster_id		= {$userdata['user_id']}
	WHERE blog_id = $blog_id
	");
}